package com.example.work3.controller;

import com.example.work3.entity.Course;
import com.example.work3.entity.CourseSelection;
import com.example.work3.entity.Major;
import com.example.work3.entity.User;
import com.example.work3.pojo.PageUser;
import com.example.work3.pojo.QueryAccount;
import com.example.work3.pojo.Result;
import com.example.work3.service.CourseSelectService;
import com.example.work3.service.MajorService;
import com.example.work3.service.UserService;
import com.example.work3.tools.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:8080",methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
@RestController
@RequestMapping("/admin")
public class MangerController {
    @Autowired
    private CourseSelectService courseSelectService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private UserService userService;
    //增加课程
    @RequestMapping("/addCourse")
    public Result addCourse(@RequestBody Course course){
        Course addCourse = courseSelectService.addORUpdateCourse(course);
        if(addCourse!=null){
            return new Result(addCourse, "200");
        }
        return new Result(null, "500");
    }
    //删除课程
    @RequestMapping("/deleteCourse")
    public Result deleteCourse(@RequestBody Course course){
        String s = courseSelectService.deleteCourse(course);
        if (s.equals("该课程已被选，无法删除")) {
            return new Result("该课程已被选，无法删除", "500");
        } else if (s.equals("true")) {
            return new Result(true, "200");
        } else {
            return new Result(false, "500");
        }
    }
    //修改课程
    @RequestMapping("/updateCourse")
    public Result updateCourse(@RequestBody Course course){
        System.out.println(course);
        Course updateCourse = courseSelectService.addORUpdateCourse(course);
        if(updateCourse!=null){
            return new Result(updateCourse, "200");
        }
        return new Result(null, "500");
    }
    //查询课程
    @RequestMapping("/selectCourse")
    public Result selectCourse(@RequestParam(name = "name",required = false) String courseName,
                               @RequestParam(name = "credits",required = false) Double credits){
        if(credits==null){
            credits=0.0;
        }
        Course course = new Course(null, courseName, null, credits);
        Result result = new Result(courseSelectService.findCourses(course), "200");
        System.out.println(result);
        return result;
    }
    //专业删除
    @DeleteMapping("/deleteMajor/{id}")
    public Result deleteMajor(@PathVariable Integer id) {
            String result = majorService.deleteMajor(id);
            if (result.equals("true")) {
                return new Result(result, "200");
            }
            return new Result(result, "500");
    }

    // 专业增加
    @PostMapping("/addMajor")
    public Result addMajor(@RequestBody Major major) {
        Major savedMajor = majorService.addOrUpdateMajor(major);
        if (savedMajor!= null) {
            return new Result(savedMajor,"200");
        }
        return new Result(null,"500");

    }

    // 专业修改
    @PutMapping("/updateMajor")
    public Result updateMajor(@RequestBody Major major) {
        Major updatedMajor = majorService.addOrUpdateMajor(major);
        if (updatedMajor != null) {
            return new Result(updatedMajor, "200");
        }
        return new Result(null,"500");
    }

    // 专业查询
    @GetMapping("/selectMajor")
    public Result selectMajor(@RequestParam (name = "name",required = false) String name) {
        List<Major> majors = majorService.findMajors(name);
        return new Result(majors,"200");
    }
    //分页查询账号
    @RequestMapping("/selectAccount")
    public Result accountManage(@RequestBody QueryAccount queryAccount){
        System.out.println(queryAccount);
        Page<User> usersByPageAndCondition = userService.getUsersByPageAndCondition(queryAccount);
        List<User> users = usersByPageAndCondition.getContent();
        long totalElements = usersByPageAndCondition.getTotalElements();
        return new Result(new PageUser(totalElements,users),"查询成功");
    }
    //新增账号
    @RequestMapping("/addAccount")
    public Result addAccount(@RequestBody QueryAccount queryAccount){
        User user = new User(queryAccount.getAccount(), queryAccount.getPassword(), Integer.parseInt(queryAccount.getRole()));
        System.out.println("add"+user);
        try {
            return new Result(userService.addUser(user), "200");
        }catch (Exception e){
            return new Result("500", "添加失败");
        }
    }
    //删除账号
    @RequestMapping("/deleteAccount/{account}")
    public Result deleteAccount(@PathVariable String account){
        System.out.println(account);
        return new Result(userService.deleteUserByAccount(account), "200");
    }
    //修改账号
    @RequestMapping("/updateAccount/{isModify}")
    public Result updateAccount(@RequestBody QueryAccount queryAccount, @PathVariable Boolean isModify){
        User user = new User();
        if(isModify){
            //修改密码
            String s = Md5.md5Encrypt(queryAccount.getPassword());
            user.setAccount(queryAccount.getAccount());
            user.setPassword(s);
            user.setRole(Integer.parseInt(queryAccount.getRole()));
        }else{
            //不修改密码
            user.setAccount(queryAccount.getAccount());
            user.setPassword(queryAccount.getPassword());
            user.setRole(Integer.parseInt(queryAccount.getRole()));
        }
        try {
            return new Result(userService.updateUser(user), "200");
        }catch (Exception e){
            return new Result("500", "修改失败");
        }
    }
    //选课查询
    @RequestMapping("/selectCourseStudent")
    public Result selectCourseStudent(@RequestBody QueryAccount queryAccount){
        System.out.println(queryAccount);
        Page<CourseSelection> coursesByPage = courseSelectService.findCoursesByPage(queryAccount);
        List<CourseSelection> content = coursesByPage.getContent();
        long totalElements = coursesByPage.getTotalElements();
        return new Result(new PageUser(totalElements,content),"查询成功");
    }
}
